package com.assignment.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import javax.swing.border.BevelBorder;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.border.EtchedBorder;

import com.assignment.database.DatabaseHandler;
import com.assignment.database.DBOperations;
import com.assignment.main.Item;
import com.assignment.main.Supplier;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class EditItemScreen extends JFrame {

	private JPanel contentPane;
	private JTextField nameText;
	private JTextField descText;
	private JTextField qtyText;
	private JTextField buyText;
	private JTextField sellText;
	final JFileChooser fc = new JFileChooser();
	JFrame thisFrame = this;
	File file;
	Item item;
	private JRadioButton rdbtnFireplace;
	private JRadioButton rdbtnStove;
	private JComboBox<Supplier> supplierCombo;
	private JComboBox styleCombo;
	private JLabel imageLabel;
	
	
	String styles[] = {"Classic","Modern","New Wave","Scandinavian","Natural","Baroque"};
	
	
	ComboBoxModel model,styleModel;
	Supplier[] suppliers;
	final SearchScreen screen;
	/**
	 * @wbp.parser.constructor
	 */
	public EditItemScreen(Item item,SearchScreen screen) {
		this(screen);
		this.item=item;
		
		
		
		nameText.setText(item.getName());
		
		if (item.getType()==Item.FIREPLACE)
			rdbtnFireplace.setSelected(true);
		else {
			rdbtnStove.setSelected(true);
		}
		
		try {
		Image i =  DBOperations.getImage(item.getId()).getImage().getScaledInstance(320, 175, 0);
        
        ImageIcon ii = new ImageIcon(i);
        
        imageLabel.setIcon(ii);
        
		}catch (Exception e) {System.out.println("Error loading image");};
		
		System.out.printf("Item: %s Supplier: %s end",item.getSupplierId(),suppliers[0].getId());
		for (Supplier s: suppliers) {
		if (item.getSupplierId().equals(s.getId())) {
			supplierCombo.setSelectedItem(s);
		}
		}
		
		
		descText.setText(item.getDescription());
		qtyText.setText(String.valueOf(item.getStock()));
		
		styleCombo.setSelectedItem(styles[item.getStyle()]);
		
		buyText.setText(String.valueOf(item.getBuyPrice()));
		sellText.setText(String.valueOf(item.getSellPrice()));
	}
	public EditItemScreen(final SearchScreen screen) {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 867, 485);
		setLocationRelativeTo(null);
		this.screen=screen;
		setVisible(true);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		panel.setBounds(10, 73, 409, 285);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblItemName = new JLabel("Name:");
		lblItemName.setBounds(10, 14, 72, 14);
		panel.add(lblItemName);
		
		JLabel lblDescription = new JLabel("Description:");
		lblDescription.setBounds(10, 99, 72, 14);
		panel.add(lblDescription);
		
		JLabel lblType = new JLabel("Type:");
		lblType.setBounds(10, 39, 46, 14);
		panel.add(lblType);
		
		rdbtnFireplace = new JRadioButton("Fireplace");
		rdbtnFireplace.setBounds(97, 35, 79, 23);
		rdbtnFireplace.setSelected(true);
		panel.add(rdbtnFireplace);
		
		rdbtnStove = new JRadioButton("Stove");
		rdbtnStove.setBounds(190, 35, 85, 23);
		panel.add(rdbtnStove);
		
		
		ButtonGroup group = new ButtonGroup();
		group.add(rdbtnFireplace);
		group.add(rdbtnStove);
		
		
		nameText = new JTextField();
		nameText.setBounds(105, 11, 115, 20);
		panel.add(nameText);
		nameText.setColumns(10);
		
		descText = new JTextField();
		descText.setBounds(105, 96, 294, 20);
		panel.add(descText);
		descText.setColumns(10);
		
		JLabel lblStockQuantity = new JLabel("Stock Quantity:");
		lblStockQuantity.setBounds(10, 135, 96, 14);
		panel.add(lblStockQuantity);
		
		qtyText = new JTextField();
		qtyText.setBounds(105, 132, 78, 20);
		panel.add(qtyText);
		qtyText.setColumns(10);
		
		JLabel lblStyle = new JLabel("Style: ");
		lblStyle.setBounds(10, 170, 72, 14);
		panel.add(lblStyle);
		
		styleCombo = new JComboBox();
		styleCombo.setBounds(107, 163, 128, 20);
		panel.add(styleCombo);
		
		styleModel = new DefaultComboBoxModel<String>(styles);
		styleCombo.setModel(styleModel);
		
		JLabel lblBuyPrice = new JLabel("Buy Price: \u00A3");
		lblBuyPrice.setBounds(10, 195, 79, 14);
		panel.add(lblBuyPrice);
		
		buyText = new JTextField();
		buyText.setBounds(105, 194, 62, 20);
		panel.add(buyText);
		buyText.setColumns(10);
		
		JLabel lblSellPrice = new JLabel("Sell Price: \u00A3");
		lblSellPrice.setBounds(177, 195, 72, 14);
		panel.add(lblSellPrice);
		
		sellText = new JTextField();
		sellText.setBounds(258, 192, 62, 20);
		panel.add(sellText);
		sellText.setColumns(10);
		
		JLabel lblSupplier = new JLabel("Supplier:");
		lblSupplier.setBounds(10, 69, 72, 14);
		panel.add(lblSupplier);
		
		supplierCombo = new JComboBox();
		supplierCombo.setBounds(105, 65, 191, 23);
		panel.add(supplierCombo);
		
		suppliers = DBOperations.getSuppliers();
		model = new DefaultComboBoxModel<Supplier>(suppliers);
		supplierCombo.setModel(model);
		
		
		
		
		
		JLabel lblEditItem = new JLabel("Edit Item");
		lblEditItem.setFont(new Font("Tahoma", Font.BOLD, 22));
		lblEditItem.setBounds(390, 26, 123, 36);
		contentPane.add(lblEditItem);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		panel_1.setBounds(460, 73, 358, 68);
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		JButton btnChooseImage = new JButton("Choose Image");
		btnChooseImage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				 int returnVal = fc.showOpenDialog(thisFrame);

			        if (returnVal == JFileChooser.APPROVE_OPTION) {
			             file = fc.getSelectedFile();
			            
			           
			            try {
							ImageIcon ii = new ImageIcon(ImageIO.read(file).getScaledInstance(320, 175, 0));
							imageLabel.setIcon(ii);
						} catch (Exception e) {
							JOptionPane.showMessageDialog(thisFrame, "Please load a valid image file");
							//e.printStackTrace();
						}
			            
			            
			           
			            thisFrame.repaint();
			            thisFrame.revalidate();
			           
			        } else {
			            System.out.println("No File Opened");
			        }
			}
		});
		btnChooseImage.setBounds(126, 13, 126, 37);
		panel_1.add(btnChooseImage);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
		panel_2.setBounds(470, 162, 348, 193);
		contentPane.add(panel_2);
		panel_2.setLayout(null);
		
		imageLabel = new JLabel("");
		imageLabel.setBounds(10, 7, 328, 175);
		panel_2.add(imageLabel);
		
		JButton btnSaveItem = new JButton("Save Item");
		btnSaveItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				
				
				String data[] = new String[10];
				
				if (item!=null) {
				data[0] = item.getId();
				}
				data[1] = nameText.getText();
				data[2] = descText.getText();
				if (rdbtnFireplace.isSelected())
				data[3] = "0";
				else data[3]="1";
				data[4] = qtyText.getText();
				data[5] = buyText.getText();
				data[6] = sellText.getText();
				data[7] = ""+styleCombo.getSelectedIndex();
				data[9] = String.valueOf(supplierCombo.getSelectedIndex()+1);
				
				try {
				Item i = new Item(data);
				i.setFile(file);
				DBOperations.saveItem(i, data[0]==null ? true : false);
				}catch (Exception e) {
					JOptionPane.showMessageDialog(thisFrame, "Error saving object, please check prices are numerical values");
					e.printStackTrace();
					return;
				}
				screen.refreshList();
				thisFrame.dispose();
				
			}
		});
		btnSaveItem.setBounds(170, 397, 123, 39);
		contentPane.add(btnSaveItem);
		
		JButton btnClear = new JButton("Clear");
		btnClear.setBounds(573, 397, 123, 39);
		contentPane.add(btnClear);
	}
}
